package com.whu.tcx01.kmeans;

import com.whu.tcx01.utils.Transform;

public class Point {
    private long nodeid;
    private double x;     // x 轴
    private double y;    // y 轴

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public Point(){

    }

    public long getNodeid() {
        return nodeid;
    }

    public void setNodeid(long nodeid) {
        this.nodeid = nodeid;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "nodeid=" + nodeid +
                ", x=" + x +
                ", y=" + y +
                '}';
    }

    /**
     * 计算距离
     *
     * @param centroid 质心点
     * @param type
     * @return
     */
    public double calculateDistance(Point centroid, int type) {
        // TODO
        double result = 0;
        switch (type) {
            case 1:
                result = calcL1Distance(centroid);
                break;
            case 2:
                result = calcCanberraDistance(centroid);
                break;
            case 3:
                //欧氏距离
                result = calcEuclidianDistance(centroid);
                break;
        }
        return result;
    }



/*
        计算距离公式
 */

    private double calcL1Distance(Point centroid) {
        double res = 0;
        res = Math.abs(getX() - centroid.getX()) + Math.abs(getY() - centroid.getY());
        return res / (double) 2;
    }

    private double calcEuclidianDistance(Point centroid) {
        // return Math.sqrt(Math.pow((centroid.getX() - getX()), 2) + Math.pow((centroid.getY() - getY()), 2));
        // 根据经纬度计算距离
        return Transform.lonLatDistance(getX(), getY(), centroid.getX(), centroid.getY());
    }

    private double calcCanberraDistance(Point centroid) {
        double res = 0;
        res = Math.abs(getX() - centroid.getX()) / (Math.abs(getX()) + Math.abs(centroid.getX()))
                + Math.abs(getY() - centroid.getY()) / (Math.abs(getY()) + Math.abs(centroid.getY()));
        return res / (double) 2;
    }

    @Override
    public boolean equals(Object obj) {
        Point other = (Point) obj;
        if (getX() == other.getX() && getY() == other.getY() && getNodeid() == other.getNodeid()) {
            return true;
        }
        return false;
    }

}
